package com.eastcom.providor.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.eastcom.bm.atom.Atom_DatasetDefine;
import com.eastcom.bm.atom.Atom_KpiDefine;
import com.eastcom.providor.dao.BaseDao;
import com.eastcom.providor.dao.IKpiDefineDao;
import com.eastcom.providor.service.extview.KpiAndThreadholdUtil;

public class KpiDefineDao extends BaseDao implements IKpiDefineDao {

	public List<Atom_KpiDefine> getAllKpiDefine() throws Exception {
		try{
			List<Atom_KpiDefine> results=new ArrayList<Atom_KpiDefine>();
			results=this.getObjects(Atom_KpiDefine.class);
			
			return results;
		}
		catch(Exception e){
			throw e;
		}
	}

	public List<Atom_KpiDefine> selectKpiDefinesByDatasetID(String datasetID) {
		return KpiAndThreadholdUtil.createKpiDefines(this.getObjects("from Atom_KpiDefine where datasetDefine.id = ?",new String[]{datasetID}));
	}

	public Atom_KpiDefine findKpiDefineByDataSetIdAndKpiId(String datasetID,String kpiId) throws Exception{
		try{
			String hql="from Atom_KpiDefine where datasetDefine.id='"+datasetID+"' and kpiId='"+kpiId+"'";
			List<Atom_KpiDefine> results=KpiAndThreadholdUtil.createKpiDefines(this.getObjects(hql));
			return results==null||results.isEmpty()?null:results.get(0);
		}
		catch(Exception e){
			throw e;
		}
	}
	
	public void saveKpiDefines(List<Atom_KpiDefine> kpiDefines) throws Exception {
		//Session session=null;
		System.out.println(kpiDefines.get(0).getKpiId());
		Atom_DatasetDefine dataset=kpiDefines.get(0).getDatasetDefine();
		System.out.println(dataset==null?null:dataset.getId());
		try{
		    //session=this.getSession();
		    this.saveObjects(kpiDefines);
		}
		catch(Exception e){
			throw e;
		}
		finally{
			//this.releaseSession(session);
		}
	}

	public void delKpiDefineByDatasetID(String datasetID) throws Exception {
		//Session session=null;
		try{
		    //session=this.getSession();
		    this.deleteObjects("delete Atom_KpiDefine where datasetDefine.id = ?", new String[]{datasetID});
		}
		catch(Exception e){
			throw e;
		}
		finally{
			//this.releaseSession(session);
		}
	}

	public void delKpiDefineByIds(List<String> IDs) throws Exception {
		try{
		    //session=this.getSession();
			StringBuffer sql=new StringBuffer("delete Atom_KpiDefine ");
			this.mergeSQLbyFieldLst(sql, "id", IDs, true);
		    this.deleteObjects(sql.toString(),null);
		}
		catch(Exception e){
			throw e;
		}
		finally{
			//this.releaseSession(session);
		}
	}
	
	public void updateKpiDefine(Atom_KpiDefine kpiDefine) throws Exception {
		try{
			this.updateObject(kpiDefine);
		}
		catch(Exception e){
			throw e;
		}
		finally{
			//this.releaseSession(session);
		}
	}
	
	public void updateKpiDefinebyDataSetId(String dataSetId,Atom_KpiDefine kpiDefine) throws Exception {
		try{
//			kpiDefine.setId(null);
		    //this.executeUpdate("delete Atom_KpiDefine where datasetDefine.id ='"+dataSetId+"' and kpiId='"+kpiDefine.getKpiId()+"'", null);
		    Atom_DatasetDefine datasetDefine=new Atom_DatasetDefine();
		    datasetDefine.setId(dataSetId);
		    kpiDefine.setDatasetDefine(datasetDefine);
		    this.saveObject(kpiDefine);
		}
		catch(Exception e){
			//tx.rollback();
			throw e;
		}
		finally{
			
		}
	}
	
	public void updateKpiDefines(String dataSetId, List<Atom_KpiDefine> kpiDefines) throws Exception {
		Session session=null;
		Transaction tx=null;
		
		try{
		    session=this.getSession();
		    tx=session.beginTransaction();
		    session.createQuery("delete Atom_KpiDefine where datasetDefine.id ='"+dataSetId+"'").executeUpdate();
		    
		    for(Atom_KpiDefine kpiDefine:kpiDefines)
		        session.save(kpiDefine);

		    tx.commit();
		}
		catch(Exception e){
			tx.rollback();
			throw e;
		}
		finally{
			this.releaseSession(session);
		}
		
	}

}
